package com.youbug.system.service.impl;

import com.youbug.system.entity.JobExecHis;
import com.youbug.system.mapper.JobExecHisMapper;
import com.youbug.system.schedule.JobExecutor;
import com.youbug.system.service.IJobExecHisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.youbug.system.entity.JobExecHis;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 定时任务执行记录表 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2023-11-12
 */
@Service
public class JobExecHisServiceImpl extends ServiceImpl<JobExecHisMapper, JobExecHis> implements IJobExecHisService {

        @Override
        public IPage<JobExecHis> getJobExecHisPage(QueryVo<JobExecHis> queryVo) {

        Page<JobExecHis> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<JobExecHis> wrapper = new QueryWrapper<>();

        JobExecHis jobExecHis =  queryVo.getPayload();

        if(!ObjectUtils.isEmpty(jobExecHis)){
            if(StringUtils.hasText(jobExecHis.getJobName())){
                wrapper.eq("job_name", jobExecHis.getJobName());
            }
            if(StringUtils.hasText(jobExecHis.getJobCode())){
                wrapper.eq("job_code", jobExecHis.getJobCode());
            }
            if(StringUtils.hasText(jobExecHis.getStatus())){
                wrapper.eq("status", jobExecHis.getStatus());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
            wrapper.orderByDesc("create_time");
        Page<JobExecHis> page = this.page(paramPage, wrapper);

        return page;
        }

}
